package com.allm.ai.api.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.authing.sdk.java.client.AuthenticationClient;
import cn.authing.sdk.java.client.ManagementClient;
import cn.authing.sdk.java.model.AuthenticationClientOptions;
import cn.authing.sdk.java.model.ManagementClientOptions;

@Configuration
public class AuthingConfig {

    @Value("${authing.app-id}")
    private String appId;

    @Value("${authing.app-secret}")
    private String appSecret;

    @Value("${authing.app-host}")
    private String appHost;

    @Value("${authing.userpool-id}")
    private String userPoolId;

    @Value("${authing.userpool-secret}")
    private String userPoolSecret;

    @Bean
    public AuthenticationClient authenticationClient() {
        AuthenticationClientOptions options = new AuthenticationClientOptions();
        options.setAppId(appId);
        options.setAppSecret(appSecret);
        options.setAppHost(appHost);
        try {
            return new AuthenticationClient(options);
        } catch (Exception e) {
            throw new RuntimeException("new AuthenticationClient failed!", e);
        }
    }

    @Bean
    public ManagementClient managementClient() {
        ManagementClientOptions clientOptions = new ManagementClientOptions();
        clientOptions.setAccessKeyId(userPoolId);
        clientOptions.setAccessKeySecret(userPoolSecret);
        try {
            return new ManagementClient(clientOptions);
        } catch (Exception e) {
            throw new RuntimeException("new ManagementClient failed!", e);
        }
    }
}
